﻿#include "FITKAbstractVAOneXMLAdaptor.h"
#include "Tools/Win64/rapidxml/rapidxml.hpp"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsNCT.h"

#include <QDebug>

void IO::FITKAbstractVAOneXMLAdaptor::consoleMessage(int level, const QString &str)
{
    //判断消息等级
    switch (level)
    {
    case 1:AppFrame::FITKMessageNormal(str); break;
    case 2: AppFrame::FITKMessageWarning(str); break;
    case 3:AppFrame::FITKMessageError(str); break;
    default: AppFrame::FITKMessageError(str); break;
    }
}

QString IO::FITKAbstractVAOneXMLAdaptor::readStrAttribute(rapidxml::xml_node<char>* node, QString attrName, bool* isOk)
{
    if (nullptr != isOk) *isOk = false;
    if (node == nullptr) return  QString();
    rapidxml::xml_attribute<>* attr = node->first_attribute(attrName.toLocal8Bit());
    if (attr == nullptr) return  QString();
    QString str = attr->value();
    if (nullptr != isOk) *isOk = true;
    return str;
}

int IO::FITKAbstractVAOneXMLAdaptor::readIntAttribute(rapidxml::xml_node<char>* node, QString attrName, bool* isOk)
{
    if (nullptr != isOk) *isOk = false;
    if (node == nullptr) return 0;
    rapidxml::xml_attribute<>* attr = node->first_attribute(attrName.toLocal8Bit());
    if (attr == nullptr) return 0;
    QString str = attr->value();
    int i = str.toInt(isOk);
    return i;
}

double IO::FITKAbstractVAOneXMLAdaptor::readDoubleAttribute(rapidxml::xml_node<char>* node, QString attrName, bool* isOk)
{
    if (nullptr != isOk) *isOk = false;
    if (node == nullptr) return 0;
    rapidxml::xml_attribute<>* attr = node->first_attribute(attrName.toLocal8Bit());
    if (attr == nullptr) return 0;
    QString str = attr->value();
    double d = str.toDouble(isOk);
    return d;
}

float IO::FITKAbstractVAOneXMLAdaptor::readFloatAttribute(rapidxml::xml_node<char>* node, QString attrName, bool * isOk)
{
    if (nullptr != isOk) *isOk = false;
    if (node == nullptr) return 0;
    rapidxml::xml_attribute<>* attr = node->first_attribute(attrName.toLocal8Bit());
    if (attr == nullptr) return 0;
    QString str = attr->value();
    float f = str.toFloat(isOk);
    return f;
}

bool IO::FITKAbstractVAOneXMLAdaptor::writeAttribute(rapidxml::xml_node<char>* node, QString attrName, QString value)
{
    if (node == nullptr) return false;
    rapidxml::xml_attribute<>* attr = node->document()->allocate_attribute(copyQStringToChar(attrName),
        copyQStringToChar(value));
    node->append_attribute(attr);
    return true;
}

bool IO::FITKAbstractVAOneXMLAdaptor::writeAttribute(rapidxml::xml_node<char>* node, QString attrName, int value)
{
    if (node == nullptr) return false;
    QString strValue = QString::number(value);
    bool b = writeAttribute(node, attrName, strValue);
    return b;
}

bool IO::FITKAbstractVAOneXMLAdaptor::writeAttribute(rapidxml::xml_node<char>* node, QString attrName, double value)
{
    if (node == nullptr) return false;
    QString strValue = QString::number(value, 'g', 8);
    bool b = writeAttribute(node, attrName, strValue);
    return b;
}

bool IO::FITKAbstractVAOneXMLAdaptor::writeAttribute(rapidxml::xml_node<char>* node, QString attrName, float value)
{
    if (node == nullptr) return false;
    QString strValue = QString::number(value);
    bool b = writeAttribute(node, attrName, strValue);
    return b;
}

QString IO::FITKAbstractVAOneXMLAdaptor::readStrNodeValue(rapidxml::xml_node<char>* node, bool* isOk)
{
    if (nullptr != isOk) *isOk = false;
    if (node == nullptr) return  QString();
    QString str = node->value();
    if (nullptr != isOk) *isOk = true;
    return str;
}

int IO::FITKAbstractVAOneXMLAdaptor::readIntNodeValue(rapidxml::xml_node<char>* node, bool* isOk)
{
    QString strValue = readStrNodeValue(node, isOk);
    int value = strValue.toInt(isOk);
    return value;
}

double IO::FITKAbstractVAOneXMLAdaptor::readDoubleNodeValue(rapidxml::xml_node<char>* node, bool* isOk)
{
    QString strValue = readStrNodeValue(node, isOk);
    double value = strValue.toDouble(isOk);
    return value;
}

float IO::FITKAbstractVAOneXMLAdaptor::readFloatNodeValue(rapidxml::xml_node<char> *node, bool *isOk)
{
    QString strValue = readStrNodeValue(node, isOk);
    float value = strValue.toFloat(isOk);
    return value;
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLAdaptor::createNodeElement(rapidxml::xml_node<char>* parentNode, QString childName, QString value /*= ""*/)
{
    if (parentNode == nullptr) return  nullptr;
    rapidxml::xml_node<char>* newNode = parentNode->document()->allocate_node(rapidxml::node_element, copyQStringToChar(childName), copyQStringToChar(value));
    parentNode->append_node(newNode);
    return newNode;
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLAdaptor::createNodeElement(rapidxml::xml_node<char>* parentNode, QString childName, int value)
{
    QString strValue = QString::number(value);
    return createNodeElement(parentNode, childName, strValue);
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLAdaptor::createNodeElement(rapidxml::xml_node<char>* parentNode, QString childName, double value)
{
    QString strValue = QString::number(value);
    return createNodeElement(parentNode, childName, strValue);
}

rapidxml::xml_node<char>* IO::FITKAbstractVAOneXMLAdaptor::createNodeElement(rapidxml::xml_node<char>* parentNode, QString childName, float value)
{
    QString strValue = QString::number(value);
    return createNodeElement(parentNode, childName, strValue);
}

void IO::FITKAbstractVAOneXMLAdaptor::readColorNodeValue(rapidxml::xml_node<char>* node, float &r, float &g, float &b)
{
    if (node != nullptr)
    {
        bool cB = true;
        bool cG = true;
        bool cR = true;
        double colorB = readFloatAttribute(node, "colorB", &cB);
        double colorG = readFloatAttribute(node, "colorG", &cG);
        double colorR = readFloatAttribute(node, "colorR", &cR);

        if (cB && cG && cR)
        {
            r = colorR;
            g = colorG;
            b = colorB;
        }
    }
}

char *IO::FITKAbstractVAOneXMLAdaptor::copyQStringToChar(const QString &qStr)
{
    QByteArray utf8Bytes = qStr.toUtf8();
    int byteCount = utf8Bytes.size();
    char* cStr = new char[byteCount + 1];
    std::memcpy(cStr, utf8Bytes.constData(), byteCount);
    cStr[byteCount] = '\0';
    return cStr;
}

QString IO::FITKAbstractVAOneXMLAdaptor::getChildElementText(rapidxml::xml_node<char>* node, QString name)
{
    for (auto child = node->first_node(); child != nullptr; child = child->next_sibling())
    {
        if (name == child->name()) {
            return child->value();
        }
    }
    return "";
}

//Acoustics::FITKAcousticsPhysics* IO::FITKAbstractVAOneXMLAdaptor::getPhysicsData()
//{
//    //获取全局数据管理器
//    auto globalData = FITKAPP->getGlobalData();

//    if(!globalData)
//    {
//        return nullptr;// 如果失败，则返回
//    }

//    //获取管理器
//    return globalData->getPhysicsData<Acoustics::FITKAcousticsPhysics>();
//}

//int IO::FITKAbstractVAOneXMLAdaptor::getAcousticsObjectIdByName(QString name)
//{
//    if (!_physics)//判断是否为空
//    {
//        _physics = getPhysicsData();

//        if(!_physics)
//        {
//            return -1;
//        }
//    }

//    auto acousticsManager = _physics->getFITKAcousticsNCTManager();

//    if(!acousticsManager)
//    {
//        return -1;
//    }

//    auto data = acousticsManager->getDataByName(name);

//    if(!data)
//    {
//        return -1;
//    }

//    return data->getDataObjectID();;
//}





